﻿using System;
using System.Web;
using System.Web.Mvc;

namespace Web.Filter
{
    public class UserPermissionsFilter : BaseFilter, IActionFilter
    {

        private const string ErrorUrl = "~/Shared/Error";

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
        }

        /// <summary>
        ///     Action访问前调用，检查Userid是否为空，为空未登录
        /// </summary>
        /// <param name="filterContext"></param>
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower() == "home" &&
                filterContext.ActionDescriptor.ActionName.ToLower() == "init")
                return;
            
            try
            {
               
                if (filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower() == "home")
                {
                    if (HttpContext.Current.Request.Cookies["user"] == null)
                    {
                        filterContext.Result = new RedirectResult(ErrorUrl);
                    }

                }

                
            }
            catch (Exception e)
            {
                
            }
        }
    }
}